package com.ys.zero.web;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.ys.zero.bean.Message;
import com.ys.zero.bean.Result;
import com.ys.zero.bean.Uarticle;
import com.ys.zero.bean.User;
import com.ys.zero.biz.ManageBackBiz;


@WebServlet("/manageBack/*")
public class manageBackServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
    
	private ManageBackBiz biz = new ManageBackBiz();
	
	/**
	 * 管理员退出
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void exit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			//移除session中loginedUser
			request.getSession().removeAttribute("loginedUser");
			toJson(response,new Result(1,"成功"));
		} catch (Exception e) {
			toJson(response,new Result(0,"功能出错"));
		}
		
	}
	/**
	 * 查询管理员
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void queryManages(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<User> manages = biz.queryManages();
		toJson(response,manages);
	}
	
	/**
	 * 添加管理员
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	protected void addManages(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, IllegalAccessException, InvocationTargetException {
		User manage = new User();
		BeanUtils.populate(manage,request.getParameterMap());
		User user = biz.addManages(manage);
		System.out.println(user.getAccount());
		System.out.println(user.getPwd());
		toJson(response, new Result(1,"成功",user));
	}
	
	
	protected void updatePwd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		String id = request.getParameter("id");
		String newPwd = request.getParameter("newPwd");
		biz.updatePwd(id,newPwd);
	}
	
}
